package com.jgp.security.controller;

import com.jgp.common.annotation.UIParam;
import com.jgp.common.utils.JGPUtil;
import com.jgp.security.secmodel.SecUser;
import com.jgp.security.service.SecUserService;
import com.jgp.sys.controller.JGPBackController;
import com.jgp.sys.ui.GridResult;
import com.jgp.sys.ui.Pager;
import com.jgp.sys.ui.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目   jgp-cloud-parent
 * 作者   loufei
 * 时间   2019-11-03
 */
@RestController
@RequestMapping("/security/user-api-ctl")
public class UseApiController extends JGPBackController {
    
    private final static List<GridResult.Category> CATEGORIES;
    
    static {
        List<GridResult.Category> categories = new ArrayList<>();
        categories.add(new GridResult.Category("姓名", "realName"));
        categories.add(new GridResult.Category("用户名", "username"));
        categories.add(new GridResult.Category("锁定状态", "locked"));
        categories.add(new GridResult.Category("邮箱", "email"));
        categories.add(new GridResult.Category("电话", "tel"));
        CATEGORIES = categories;
    }
    
    @Autowired
    private SecUserService userService;
    
    /**
     * 保存系统管理员
     *
     * @param user
     * @return
     */
    @RequestMapping("save-system-admin")
    public Result saveSystemAdmin(SecUser user) {
        userService.saveSystemAdmin(user);
        return ajaxRe(true);
    }
    
    /**
     * 保存系统用户
     *
     * @param user
     * @return
     */
    @RequestMapping("save-system-user")
    public Result saveSystemUser(SecUser user) {
        userService.saveSystemUser(user);
        return ajaxRe(true);
    }
    
    /**
     * 查询系统管理员
     *
     * @param realName 真实姓名【like】
     * @param username 用户名【like】
     * @param tel      电话【likeL】
     * @param email    邮箱【like】
     * @param pager    分页
     * @return
     */
    @RequestMapping("query-system-admin")
    public Result querySystemAdmin(String realName, String username, String tel, String email,Boolean locked, @UIParam("pager") Pager pager) {
        List<SecUser> list = userService.querySystemAdmin(realName, username, tel, email,locked, pager);
        return ajaxReGrid("gdata", CATEGORIES, list, pager);
    }
    
    /**
     * 查询系统用户
     *
     * @param institutionId 机构【eq】
     * @param realName 真实姓名【like】
     * @param username 用户名【like】
     * @param tel      电话【likeL】
     * @param email    邮箱【like】
     * @param pager    分页
     * @return
     */
    @RequestMapping("query-system-user")
    public Result querySystemUser(Long institutionId, String realName, String username, String tel, String email,Boolean locked, @UIParam("pager") Pager pager) {
        List<SecUser> list = userService.querySystemUser(institutionId,realName, username, tel, email,locked, pager);
        return ajaxReGrid("gdata", CATEGORIES, list, pager);
    }
    
    /**
     * 查询指定人
     *
     * @param id 用户ID
     * @return
     */
    @RequestMapping("query-user-one")
    public Result queryUserOne(String id) {
        return ajaxReData("user", userService.query(id));
    }
    
    /**
     * 批量删除系统管理员
     *
     * @param ids
     * @return
     */
    @RequestMapping("remove-system-admin")
    public Result removeSystemAdmin(@RequestBody List<String> ids) {
        userService.removeUser(ids);
        return ajaxRe(true);
    }
    
    /**
     * 批量删除系统用户
     *
     * @param ids
     * @return
     */
    @RequestMapping("remove-system-user")
    public Result removeSystemUser(@RequestBody List<String> ids) {
        userService.removeUser(ids);
        return ajaxRe(true);
    }
    
    @RequestMapping("reset-password")
    public Result resetPassword(String userId) {
        String pwd = JGPUtil.randomStr(6);
        userService.resetPassword(userId,pwd);
        return ajaxReData("pwd",pwd);
    }
    
    @RequestMapping("lock")
    public Result lock(String userId) {
        userService.lock(userId);
        return ajaxRe(true);
    }
    
    @RequestMapping("un-lock")
    public Result unLock(String userId) {
        userService.unLock(userId);
        return ajaxRe(true);
    }
    
    
}
